<?php
include '../classes/Authentication.php';
include '../classes/HTML.php';
session_start ();
$auth = new Authentication ();
$auth->checkAuthentication ($_SERVER['PHP_SELF'], 2);
HTML::createHeader('Benutzer erstellen', 'users_create.css');
$db = new DB_For_User();
?>
<body>
	<?php HTML::createLogobar($auth->getUser());?>
	<div class="container">
		<div class="row">
			<div class="col-md-3">
				<h3>Infos</h3>
				<p>Zur&uuml;ck zur &Uuml;bersicht:</p>
				<form method="post" action="users.php">
					<button type="submit" name="back" class="btn btn-default">Zur&uuml;ck &raquo;</button>
				</form>
			</div>
			<div class="col-md-4">
				<h3>User</h3>
				<?php 
					if(isset($_POST['submit'])) {
						if($_POST['password'] == $_POST['password2']) {
							if($db->createUser($_POST['email'], $_POST['password'], $_POST['grant']))
								header('Location: users.php');
							else 
								echo '<p style="color: red;">Diese E-Mail ist bereits vergeben!</p>';
						} else {
							echo '<p style="color: red;">Passw&ouml;rter stimmen nicht &uuml;berein!</p>';
						}
					}
				?>
				<form method="post" action="users_create.php">
					<div class="form-group">
						<!-- wenn fertig: type="email" placeholder="Email" -->
						E-Mail <input type="email" name="email" placeholder="Email" value="<?php if(isset($_POST['email'])) echo $_POST['email'] ?>" class="form-control" required>
						Passwort <input type="password" name="password" placeholder="Passwort" class="form-control" required>
						Passwort wiederholen <input type="password" name="password2" placeholder="Passwort wiederholen" class="form-control" required>
						Rechte<br />
						<select name="grant">
							<option value="0" <?php if(isset($_POST['grant'])) { if($_POST['grant'] == 0) { echo 'selected="selected"';} }?>>Visitor</option>
							<option value="1" <?php if(isset($_POST['grant'])) { if($_POST['grant'] == 1) { echo 'selected="selected"';} }?>>Inventor</option>
						</select>
					</div>
					<button type="submit" name="submit" class="btn btn-default">Erstellen &raquo;</button>
				</form>
			</div>
		</div>
		<?php HTML::createFooter();?>
	</div>
	<?php HTML::includeJS();?>
</body>
</html>